#include "TestState.h"
#include "DX11ErrorReport.h"
#include "DX11HelpfulFunctions.h"
#include "Timer.h"
#include "CameraManager.h"
#include "LightingManager.h"
#include "OOBB.h"
#include "Vec3.h"
#include "Game.h"
#include "AntTweakBarManager.h"
#include "DX11TextureManager.h"
#include "DX11HeightMapTerrain.h"
#include "Agent.h"
#include "PhysicsParticle.h"
#include "Spring.h"
#include "DX11ShaderManager.h"
#include "AgentGroup.h"
#include "Skydome.h"
//#include "DX11InputManager.h"
#include "Text.h"
#include "UIChat.h"
#include "AIRoom.h"
#include "Maze.h"
#include "ConfigManager.h"
#include "Get3DMousePos.h"
#include "MiniMap.h"
#include "OptionsState.h"
#include "InfoState.h"

#include <MessageQueue.h>
#include <ClientPlayerManager.h>

ID3D11Texture2D* pRenderTargetTexture2;
ID3D11RenderTargetView* pRenderTargetView2;
ID3D11ShaderResourceView* pShaderResourceView2;

TestState::TestState()
{
	m_rotCrateAngle = 0;
	for (int i = 0 ; i != 15; i++)
	{
		axis[i].x = 0;
		axis[i].y = 0;
		axis[i].z = 0;
	}


	pRenderTargetTexture2 = NULL;
	pRenderTargetView2 = NULL;
	pShaderResourceView2 = NULL;


}

TestState::~TestState()
{

}

void TestState::DrawOptionsView()
{
	DX11::ClearColour(0.5f, 0.5f, 0.5f, 1.0f);
	
	TheMiniMap::Instance()->RenderMap();	
	
	TheScreenManager::Instance()->GetDeviceContext()->OMSetRenderTargets(1, 
		TheScreenManager::Instance()->GetRenderTargetview(),
		TheScreenManager::Instance()->GetDepthStencilView());		
	DX11::ClearColour(0.0f, 0.0f, 0.0f, 1.0f);
	TheCameraManager::Instance()->Update();
	
	TheMiniMap::Instance()->DrawColourPick();
	DX11::ClearColour(0.0f, 0.0f, 0.0f, 1.0f);

	TheCameraManager::Instance()->Draw();
	TheGame::Instance()->DrawGameObjects();

	TheDX11ShaderManager::Instance()->ResetResources();
}

void TestState::Draw()
{	
	TheScreenManager::Instance()->GetDeviceContext()->OMSetRenderTargets(1, 
		TheScreenManager::Instance()->GetRenderTargetview(),
		TheScreenManager::Instance()->GetDepthStencilView());		

	TheCameraManager::Instance()->Update();	
	DX11::ClearColour(0.0f, 0.0f, 0.0f, 1.0f);

	TheObjMeshManager::Instance()->Translate("floor", D3DXVECTOR3(0.0f, -0.5f, 0.0f));
	TheObjMeshManager::Instance()->Draw("floor");

	TheCPManager::Instance()->Draw();

	TheDX11ShaderManager::Instance()->ResetResources();
}

void TestState::Update()
{
	TheCameraManager::Instance()->Update();
	TheCPManager::Instance()->Update();
}

void TestState::OnKeyDownEvent(const WPARAM& event)
{
	if (event == VK_RETURN)
	{
		TheGame::Instance()->SetGameState(TheInfoState::Instance());
	}
	else if (event == VK_ESCAPE)
	{
		TheGame::Instance()->SetGameState(TheOptionsState::Instance());
	}
}

void TestState::OnMouseMotionEvent(const LPARAM& event)
{
	int x = LOWORD(event);
	int y = HIWORD(event);
	
	int screenWidth = TheScreenManager::Instance()->GetWindowWidth();
	int screenHeight = TheScreenManager::Instance()->GetWindowHeight();
	
	if (x <= screenWidth && x >= 0)
	{
		m_mouseX = (float)x;
	}
	if (y <= screenHeight && y >= 0)
	{
		m_mouseY = (float)y;
	}
}
void TestState::OnMouseButtonDownEvent(const MSG& event)
{
	m_mouseDown = true;
}

void TestState::OnMouseButtonUpEvent(const MSG& event)
{
	m_mouseDown = false;
}

void TestState::OnActivated()
{
	TheEventPoller::Instance()->AddHandler(this);
}

void TestState::OnDeactivated()
{
	TheEventPoller::Instance()->RemoveHandler(this);
}

void TestState::GetAxisArray(Vec3f axes[])
{
	for (int i = 0; i != 15; i++)
	{
		axis[i] = axes[i];
	}
}
